/************************************************************************
  * @file:     xxx.c
  * @author:   guangjieMVP
  * @github:   https://github.com/guangjieMVP
  * @version:  v1.0.0
  * @date:     2020-xx-xx
  * @brief:               
**************************************************************************/

/************************************************************************
  * @file:     xxx.c
  * @author:   guangjieMVP
  * @github:   https://github.com/guangjieMVP
  * @version:  v1.0.0
  * @date:     2020-xx-xx
  * @brief:               
**************************************************************************/
#include "stack_normal.h"

/************************************************************************
  * @brief:         入栈函数
  * @param[in]:     None
  * @retval[out]:   None
  * @note:            
  * @author:        guangjieMVP
**************************************************************************/
static int _push(abstract_stack_t *abs, int val)
{
    if(!abs)
        return -1;
    
    stack_normal_t *s = (stack_normal_t *)abs;
    s->stack_buffer[s->top++] = val;
    return 0;
}

static int _pop(abstract_stack_t *abs, int *val)
{
    if(!abs)
        return -1;
    
    stack_normal_t *s = (stack_normal_t *)abs;
    *val = s->stack_buffer[s->top--];
    return 0;
}

/************************************************************************
  * @brief:         出栈函数    
  * @param[in]:     None
  * @retval[out]:   None
  * @note:            
  * @author:        guangjieMVP
**************************************************************************/
int stack_normal_init(stack_normal_t *s, int *s_buffer, int size)
{
    if(!s || !s_buffer || size <= 0)
        return -1;

    abstract_stack_init(&s->super, _push, _pop);               //初始化父类抽象栈
    s->size         = size;
    s->top          = 0;
    s->stack_buffer = s_buffer;
    return 0;
}

int stack_normal_deinit(stack_normal_t *s)
{
    if(!s)
        return -1;
    
    memset(s->stack_buffer, 0, s->size);
    s->size = 0;
    s->top  = 0;
    abstract_stack_deinit(&s->super);
}

int stack_normal_push(stack_normal_t *s, int val)
{
    if(!s)
        return -1;
    abstract_stack_t *abs = (abstract_stack_t *)s;
    return abs->push(abs, val);
}

int stack_normal_pop(stack_normal_t *s, int *val)
{
    if(!s || !val)
        return -1;
    
    abstract_stack_t *abs = (abstract_stack_t *)s;
    return abs->pop(abs, val);
}

